﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Configuration;

public partial class LienHe : System.Web.UI.Page
{
    public string noidung = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            int lang = 2;
            if (Request.QueryString["lang"] != "" && Request.QueryString["lang"] != null) lang = Convert.ToInt32(Request.QueryString["lang"]);
            lbPageName.Text = "Contact us";
            if (lang == 1) lbPageName.Text = "Liên hệ";
            TrangTinhDataSet TrangTinhds = new TrangTinhDataSet();
            TrangTinhHelper TrangTinhhelper = new TrangTinhHelper();
            TrangTinhds = TrangTinhhelper.LoadTrangTinhViTri("LienHe", lang);
            if (!TrangTinhds.TrangTinh[0].IsNoiDungNull()) noidung = TrangTinhds.TrangTinh[0].NoiDung;

            lbTieuDe.Text = "Subject:";
            if (lang == 1) lbTieuDe.Text = "Tiêu đề:";
            lbTenCongTy.Text = "Your Company:";
            if (lang == 1) lbTenCongTy.Text = "Tên công ty:";
            lbDiaChi.Text = "Your Address:";
            if (lang == 1) lbDiaChi.Text = "Địa chỉ:";
            lbSoDienThoai.Text = "Your Phone Number:";
            if (lang == 1) lbSoDienThoai.Text = "Số điện thoại:";
            lbEmail.Text = "Your Email:";
            if (lang == 1) lbEmail.Text = "Email của bạn:";
            lbNoiDung.Text = "Content:";
            if (lang == 1) lbNoiDung.Text = "Nội dung góp ý:";
            lbMaXacNhan.Text = "Captcha:";
            if (lang == 1) lbMaXacNhan.Text = "Mã xác nhận:";
            btnGui.Text = "Send";
            if (lang == 1) btnGui.Text = "Gửi";

        }
    }

    protected void btnGui_Click(object sender, EventArgs e)
    {
        if (rpHash(Request.Form["txtMaXacNhan"]) == Request.Form["txtMaXacNhanHash"])
        {
            if (Send_Email(txtEmail.Text, "info@2alo.com", txtTenCongTy.Text + " - " + txtTieuDe.Text, txtNoiDung.Text + "<br /><br />--------------------------------------------------<br />Tên: " + txtTenCongTy.Text + "<br />Địa chỉ: " + txtDiaChi.Text + "<br />Điện thoại: " + txtSoDienThoai.Text + "<br />Email: " + txtEmail.Text, "mail.2alo.com"))
            {
                ClientScript.RegisterStartupScript(typeof(Page), "testScript", "thanhcong()", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(typeof(Page), "testScript", "thatbai()", true);

            }
        }
        else
        {
            lblthanhpho0.Visible = true;
            lblthanhpho0.ForeColor = System.Drawing.Color.Red;
            lblthanhpho0.Font.Bold = true;
            return;
        }
    }

    private string rpHash(string value)
    {
        int hash = 5381;
        value = value.ToUpper();
        for (int i = 0; i < value.Length; i++)
        {
            hash = ((hash << 5) + hash) + value[i];
        }
        return hash.ToString();
    }

    public static bool Send_Email(string emailfrom, string emailto, string subject, string body, string smtphost)
    {
        bool success = false;
        try
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(emailfrom);
            message.To.Add(new MailAddress(emailto));
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = true;
            SmtpClient client = new SmtpClient();
            client.Host = smtphost;
            client.Send(message);
            success = true;
        }
        catch (Exception ex)
        {
        }
        return success;
    }
}